cmake_minimum_required(VERSION 3.10)

project(rubberband)

# Set include and source paths
set(RUBBERBAND_PATH ${CMAKE_SOURCE_DIR}/rubberband)
set(RUBBERBAND_SRC_PATH ${CMAKE_SOURCE_DIR}/src)

# Define the source files
set(RUBBERBAND_SRC_FILES
    ${RUBBERBAND_SRC_PATH}/rubberband-c.cpp
    ${RUBBERBAND_SRC_PATH}/RubberBandStretcher.cpp
    ${RUBBERBAND_SRC_PATH}/RubberBandLiveShifter.cpp
    ${RUBBERBAND_SRC_PATH}/faster/AudioCurveCalculator.cpp
    ${RUBBERBAND_SRC_PATH}/faster/CompoundAudioCurve.cpp
    ${RUBBERBAND_SRC_PATH}/faster/HighFrequencyAudioCurve.cpp
    ${RUBBERBAND_SRC_PATH}/faster/SilentAudioCurve.cpp
    ${RUBBERBAND_SRC_PATH}/faster/PercussiveAudioCurve.cpp
    ${RUBBERBAND_SRC_PATH}/faster/R2Stretcher.cpp
    ${RUBBERBAND_SRC_PATH}/faster/StretcherChannelData.cpp
    ${RUBBERBAND_SRC_PATH}/faster/StretcherProcess.cpp
    ${RUBBERBAND_SRC_PATH}/common/Allocators.cpp
    ${RUBBERBAND_SRC_PATH}/common/BQResampler.cpp
    ${RUBBERBAND_SRC_PATH}/common/FFT.cpp
    ${RUBBERBAND_SRC_PATH}/common/Log.cpp
    ${RUBBERBAND_SRC_PATH}/common/Profiler.cpp
    ${RUBBERBAND_SRC_PATH}/common/Resampler.cpp
    ${RUBBERBAND_SRC_PATH}/common/StretchCalculator.cpp
    ${RUBBERBAND_SRC_PATH}/common/sysutils.cpp
    ${RUBBERBAND_SRC_PATH}/common/mathmisc.cpp
    ${RUBBERBAND_SRC_PATH}/common/Thread.cpp
    ${RUBBERBAND_SRC_PATH}/finer/R3Stretcher.cpp
    ${RUBBERBAND_SRC_PATH}/finer/R3LiveShifter.cpp
)

# Create static library
add_library(rubberband STATIC ${RUBBERBAND_SRC_FILES})

# Add target include directories
target_include_directories(rubberband PRIVATE
    ${RUBBERBAND_PATH}
    ${RUBBERBAND_SRC_PATH}
)

# Add C++ flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -DUSE_BQRESAMPLER -DUSE_BUILTIN_FFT -DLACK_POSIX_MEMALIGN -DUSE_OWN_ALIGNED_MALLOC -DLACK_SINCOS -DNO_EXCEPTIONS -DNO_THREADING -DNO_THREAD_CHECKS -DUSE_PTHREADS -DNO_TIMING -DNO_TIMING_COMPLETE_NOOP -O3 -ffast-math -ftree-vectorize")

# Install static library and header files
install(TARGETS rubberband DESTINATION lib)
install(DIRECTORY ${RUBBERBAND_PATH}/ DESTINATION include/rubberband)

# Configure pkg-config file 
set(PKGCONFIG_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig)
configure_file(${CMAKE_SOURCE_DIR}/rubberband.pc.in ${CMAKE_BINARY_DIR}/rubberband.pc @ONLY)
file(READ "${CMAKE_BINARY_DIR}/rubberband.pc" content)
string(REPLACE "%PREFIX%" "${CMAKE_INSTALL_PREFIX}" content "${content}")
string(REPLACE "%VERSION%" "${PKG_VER}" content "${content}")
file(WRITE "${CMAKE_BINARY_DIR}/rubberband.pc" "${content}")

# Install the pkg-config file
install(FILES ${CMAKE_BINARY_DIR}/rubberband.pc DESTINATION ${PKGCONFIG_INSTALL_DIR})
